﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Shell;
using C1.Phone.Chart;

namespace ControlExplorer.Controls.Chart
{
    public partial class Selection : UserControl, IExposeApplicationBarItems
    {
        Random rnd = new Random();
        ApplicationBarIconButton btnColumn = new ApplicationBarIconButton(new Uri("Resources/AppBar/chart_column.png", UriKind.Relative));
        ApplicationBarIconButton btnLine = new ApplicationBarIconButton(new Uri("Resources/AppBar/chart_line.png", UriKind.Relative));
        ApplicationBarIconButton btnPie = new ApplicationBarIconButton(new Uri("Resources/AppBar/chart_pie.png", UriKind.Relative));

        public Selection()
        {
            InitializeComponent();

            btnColumn.Text = "Column";
            btnColumn.Click += btnColumn_Click;

            btnLine.Text = "Line";
            btnLine.Click += btnLine_Click;

            btnPie.Text = "Pie";
            btnPie.Click += btnPie_Click;

            // create sample data
            var items = new CollectionView<Product>(); ;
            for (int i = 0; i < 5; i++)
                items.Add(new Product() { Name = "Product " + i, Price = rnd.Next(5, 50) });

            chart.BeginUpdate();
            // set data source
            chart.Data.ItemsSource = items;
            chart.EndUpdate();
        }

        void btnColumn_Click(object sender, EventArgs e)
        {
            chart.ChartType = ChartType.Column;
            btnColumn.IsEnabled = false;
            btnPie.IsEnabled = true;
            btnLine.IsEnabled = true;
        }

        void btnLine_Click(object sender, EventArgs e)
        {
            chart.ChartType = ChartType.LineSymbols;
            btnColumn.IsEnabled = true;
            btnPie.IsEnabled = true;
            btnLine.IsEnabled = false;
        }

        void btnPie_Click(object sender, EventArgs e)
        {
            chart.ChartType = ChartType.Pie;
            btnColumn.IsEnabled = true;
            btnPie.IsEnabled = false;
            btnLine.IsEnabled = true;
        }

        #region IExposeApplicationBarItems Members

        public IEnumerable<IApplicationBarMenuItem> ApplicationBarItems
        {
            get
            {
                yield return btnColumn;
                yield return btnLine;
                yield return btnPie;
            }
        }

        #endregion

    }

    public class Product
    {
        public string Name { get; set; }
        public double Price { get; set; }
    }

    public class CollectionView<T> : ObservableCollection<T>, ICollectionView
    {
        int currentPosition = -1;
        object currentItem = null;

        public bool CanFilter
        {
            get { throw new NotImplementedException(); }
        }

        public bool CanGroup
        {
            get { throw new NotImplementedException(); }
        }

        public bool CanSort
        {
            get { throw new NotImplementedException(); }
        }

        public bool Contains(object item)
        {
            throw new NotImplementedException();
        }

        public System.Globalization.CultureInfo Culture
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public event EventHandler CurrentChanged;

        public event CurrentChangingEventHandler CurrentChanging;

        public object CurrentItem
        {
            get { return currentItem; }
        }

        public int CurrentPosition
        {
            get { return currentPosition; }
        }

        public IDisposable DeferRefresh()
        {
            throw new NotImplementedException();
        }

        public Predicate<object> Filter
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public ObservableCollection<GroupDescription> GroupDescriptions
        {
            get { throw new NotImplementedException(); }
        }

        public ReadOnlyObservableCollection<object> Groups
        {
            get { throw new NotImplementedException(); }
        }

        public bool IsCurrentAfterLast
        {
            get { throw new NotImplementedException(); }
        }

        public bool IsCurrentBeforeFirst
        {
            get { throw new NotImplementedException(); }
        }

        public bool IsEmpty
        {
            get { return Count == 0; }
        }

        public bool MoveCurrentTo(object item)
        {
            if (item != null && item is T)
            {
                var index = IndexOf((T)item);
                MoveCurrentToPosition(index);
                return true;
            }
            return false;
        }

        public bool MoveCurrentToFirst()
        {
            return MoveCurrentToPosition(0);
        }

        public bool MoveCurrentToLast()
        {
            return MoveCurrentToPosition(Count - 1);
        }

        public bool MoveCurrentToNext()
        {
            throw new NotImplementedException();
        }

        public bool MoveCurrentToPosition(int position)
        {
            if (position >= 0 && position < Count)
            {
                if (CurrentChanging != null)
                    CurrentChanging(this, new CurrentChangingEventArgs());

                currentPosition = position;
                currentItem = this[position];

                if (CurrentChanged != null)
                    CurrentChanged(this, EventArgs.Empty);
                return true;
            }

            return false;
        }

        public bool MoveCurrentToPrevious()
        {
            throw new NotImplementedException();
        }

        public void Refresh()
        {
            throw new NotImplementedException();
        }

        public SortDescriptionCollection SortDescriptions
        {
            get { throw new NotImplementedException(); }
        }

        public System.Collections.IEnumerable SourceCollection
        {
            get { throw new NotImplementedException(); }
        }
    }
}
